package com.kye.express.business.waybill_take_photo.history;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.online_pay.detail.OnlinePayDetailActivity;
import com.kye.express.databinding.ItemOnlinePayHistoryBinding;
import com.kye.express.databinding.ItemWaybillTakePhotoHistoryBinding;
import com.kye.model.bean.online.GetPayRecordResultBean;
import com.kye.model.bean.waybill_take_photo.QueryWaybillTakePhotoHistoryResultBean;

import io.reactivex.functions.Consumer;

class WaybillTakePhotoHistoryListItemViewBinder extends me.drakeet.multitype.ItemViewBinder<QueryWaybillTakePhotoHistoryResultBean.DatasBean, WaybillTakePhotoHistoryListItemViewBinder.ViewHolder> {

    private Context mContext;
    WaybillTakePhotoHistoryActivityContract.WaybillTakePhotoHistoryActivityViewPresenter mPresenter;

    public WaybillTakePhotoHistoryListItemViewBinder(Context context,WaybillTakePhotoHistoryActivityContract.WaybillTakePhotoHistoryActivityViewPresenter presenter) {
        mContext = context;
        mPresenter = presenter;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_waybill_take_photo_history, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull QueryWaybillTakePhotoHistoryResultBean.DatasBean item) {
        holder.bindData(item);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private QueryWaybillTakePhotoHistoryResultBean.DatasBean data;
        ItemWaybillTakePhotoHistoryBinding dataBinding;

        public ViewHolder(View itemView) {
            super(itemView);
            dataBinding = DataBindingUtil.bind(itemView);
        }


        public void bindData(QueryWaybillTakePhotoHistoryResultBean.DatasBean item) {
            data = item;
            if (data != null) {
                dataBinding.setData(data);
                RxView.clicks(dataBinding.tvPicture)
                        .compose(new DebounceObservableTransformer<>())
                        .subscribe(new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Exception {
                                if (mPresenter != null){
                                    mPresenter.getPicture(data.getWaybill());
                                }
                            }
                        });
            }
        }
    }
}
